#include "ProductTestVideo.h"

#define DEF_TAG             "TestVideo"

extern Com_u8 g_ProductTest_Media_Running;
extern Com_s32 g_ProductTest_ThreadNum;

Com_pvoid Ext_ProductTest_Video_Thread(Com_pvoid arg)
{
    Com_s32 i;
    Com_s32 ret;
    pStSdkRingBufInfoProducer pro;
    pStSdkRingBufInfo ring_buff;
    StSdkVideoInfo info;
    StSdkRingBufFrame frame;

    memset(&info,0,sizeof(info));

    pro = (pStSdkRingBufInfoProducer)arg;
    ring_buff = &pro->video_ring_buffer;

    ret = Ext_SdkVideo_Init();
    if (ret != DEF_COM_SUCCESS)
    {
        PRT_ERR(DEF_TAG,"video init failed !\n");
        return NULL;
    }

    Ext_SdkVideo_Start();
    Ext_SdkOsd_SetStatus(DEF_COM_TRUE);

    PRT_NORMAL("start video !!\n");
    g_ProductTest_ThreadNum++;

    while(g_ProductTest_Media_Running)
    {
        for(i = 0;i < 2;i++)
        {
            ret = Ext_SdkMediaCom_GetVideoData(i,&info);
            if (ret != DEF_COM_SUCCESS)
            {
                continue;
            }

            if(i == 0)
            {
                memset(&frame, 0, sizeof(StSdkRingBufFrame));

                frame.frame_buf = (Com_ps8)info.buf;
                frame.frame_len = info.size;
                frame.frame_type = (info.type == 1) ? 1 : 0;

                ret = pro->push_video_frame_to_ring_buffer(ring_buff,&frame);
                if(ret == -3)
                {
                    Ext_SdkMediaCom_RequestIDR();
                }
            }

            Ext_SdkMediaCom_ReleaseVideoData(i);
        }
    }

    ret = Ext_SdkVideo_Exit();
    if(ret != DEF_COM_SUCCESS)
    {
        PRT_ERR(DEF_TAG,"video exit failed !\n");
        return NULL;
    }

    g_ProductTest_ThreadNum--;
    pthread_exit(0);
}
